package com.wakala.employment.fragment.other;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.wakala.employment.R;
import com.wakala.employment.core.BaseFragment;
import com.wakala.employment.databinding.FragmentRealNameAuthBinding;
import com.wakala.employment.utils.RegexUtils;
import com.wakala.employment.utils.Utils;
import com.wakala.employment.utils.XUtils;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xutil.tip.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link RealNameAuthFragment#} factory method to
 * create an instance of this fragment.
 */
@Page(anim = CoreAnim.none)
public class RealNameAuthFragment extends BaseFragment<FragmentRealNameAuthBinding> implements View.OnClickListener  {

    private List<LocalMedia> mSelectListZm = new ArrayList<>();
    private LocalMedia localMediaZm;
    private List<LocalMedia> mSelectListFm = new ArrayList<>();
    private LocalMedia localMediaFm;

    @NonNull
    @Override
    protected FragmentRealNameAuthBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentRealNameAuthBinding.inflate(inflater, container, attachToRoot);
    }

    @Override
    protected TitleBar initTitle() {
        return super.initTitle().setTitle("实名认证");
    }

    @Override
    protected void initListeners() {
        binding.toStep1.setOnClickListener(this);
        binding.toStep2.setOnClickListener(this);
        binding.toSubmit.setOnClickListener(this);
        binding.radiusImageViewZm.setOnClickListener(this);
        binding.radiusImageViewFm.setOnClickListener(this);
        binding.tvToRealNameProtocol.setOnClickListener(this);
    }

    @Override
    protected void initViews() {
        binding.ltIdCard.setVisibility(View.GONE);
        binding.ltIdentity.setVisibility(View.VISIBLE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.color.color_f4)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择
                    mSelectListZm = PictureSelector.obtainMultipleResult(data);
                    localMediaZm= mSelectListZm.get(0);
                    Glide.with(binding.radiusImageViewZm.getContext()).load(localMediaZm.getCompressPath())
                            .apply(options)
                            .into(binding.radiusImageViewZm);
                    break;
                case PictureConfig.CHOOSE_REQUEST+1:
                    // 图片选择
                    mSelectListFm = PictureSelector.obtainMultipleResult(data);
                    localMediaFm = mSelectListFm.get(0);
                    Glide.with(binding.radiusImageViewFm.getContext()).load(localMediaFm.getCompressPath())
                            .apply(options)
                            .into(binding.radiusImageViewFm);
                    break;
                default:
                    break;
            }
        }
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.to_step2){
            if(binding.stvName.getCenterEditValue().length()==0){
                ToastUtils.toast("请填写姓名");
                return;
            }else if(binding.stvName.getCenterEditValue().length()>10){
                ToastUtils.toast("填写姓名超长，请重新填写");
                return;
            } else if (!RegexUtils.checkIdCard(binding.stvIdNumber.getCenterEditValue())) {
                ToastUtils.toast("身份证号码错误，请重新填写");
                return;
            }
            binding.ltIdentity.setVisibility(View.GONE);
            binding.ltIdCard.setVisibility(View.VISIBLE);
        }else if(id == R.id.to_step1){
            binding.ltIdentity.setVisibility(View.VISIBLE);
            binding.ltIdCard.setVisibility(View.GONE);
        }else if(id ==R.id.radiusImageView_zm){
            //选择并上传正面身份证
            mSelectListZm.clear();
            XUtils.getPictureSelector(this)
                    .selectionMedia(mSelectListZm)
                    .maxSelectNum(1)
                    .selectionMode(PictureConfig.SINGLE)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
        }else if(id == R.id.radiusImageView_fm){
            //选择并上传身份证反面
            mSelectListFm.clear();
            XUtils.getPictureSelector(this)
                    .selectionMedia(mSelectListFm)
                    .maxSelectNum(1)
                    .selectionMode(PictureConfig.SINGLE)
                    .forResult(PictureConfig.CHOOSE_REQUEST+1);
        }else if(id == R.id.tv_to_real_name_protocol){
            Utils.goWeb(getContext(), PrivacyPolicyFragment.REAL_NAME_AUTH_URL);
        }
    }
}